﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Benefit.ViewModel
{
    /// <summary>
    /// 左侧菜单显示信息
    /// </summary>
    public class LeftMenuInfos
    {
        /// <summary>
        /// 得到分析系统当前所有的组
        /// </summary>
        public List<Models.Sys.Groups> Groups { get; set; }
        /// <summary>
        /// 得到分析系统当前所有的操作员
        /// </summary>
        public List<Models.Sys.Operator> Operators { get; set; }
        /// <summary>
        /// 得到当前有效的小组分组情况(以最后一个交易日为准)
        /// </summary>
        public List<Models.Sys.DayGroupOperators> DayGroupOperators { get; set; }
        /// <summary>
        /// 当前登录用户(操作员)
        /// </summary>
        public Models.Sys.Operator Operator { get; set; }
        /// <summary>
        /// 当前登录用户当月收益情况
        /// </summary>
        public Models.View.OperatorMonthSum OperatorMonthSum { get; set; }
        /// <summary>
        /// 当前登录用户上月收益情况
        /// </summary>
        public Models.View.OperatorMonthSum PreOperatorMonthSum { get; set; }
        /// <summary>
        /// 当前登录用户当月预期值
        /// </summary>
        public double LoginOperatorMonthExpected { get; set; }

        public Models.Sys.OperatorInfo OperatorInfo { get; set; }
        /// <summary>
        /// 用户所在的组
        /// </summary>
        public Models.Sys.Groups UserGroup { get; set; }
        /// <summary>
        /// 公司现时周排名
        /// </summary>
        public int Sort { get; set; }
        /// <summary>
        /// 成长指数
        /// </summary>
        public int GrowthIndex { get; set; }
        /// <summary>
        /// 稳定指数
        /// </summary>
        public int StableIndex { get; set; }
        /// <summary>
        /// 登录用户类型
        /// </summary>
        public int LoginUserType { get; set; }
        /// <summary>
        /// 稳定小叶子数量
        /// </summary>
        public int StableCount { get; set; }
        /// <summary>
        /// 成长小叶子数量
        /// </summary>
        public int GrowthCount { get; set; }
        /// <summary>
        /// 进度条长度
        /// </summary>
        public int[] Lengths { get; set; }


        private Interface.Sys.ITradeHistory ITradeHistory = null;
        private Interface.Sys.IOperatorCouldSeeGroups IOperatorCouldSeeGroups = null;
        private Interface.Sys.IOperator IOperator = null;
        private Interface.Sys.IDayGroupOperators IDayGroupOperators = null;
        private Interface.Sys.IOperatorInfo IOperatorInfo = null;
        private Interface.Week.IOperatorWeekSort IOperatorWeekSort = null;
        private Interface.View.IOperatorMonthSum IOperatorMonthSum = null;
        private Interface.View.IOperatorMonthExpected IOperatorMonthExpected = null;

        public LeftMenuInfos(DB.DBManager db, int tradeHistoryId, int operatorId)
        {
            InitInterface(db);
            Models.Sys.TradeHistory th = ITradeHistory.GetModel(tradeHistoryId);
            this.Groups = IOperatorCouldSeeGroups.GetOperatorCouldSeeGroups(operatorId);
            this.Operators = IOperator.GetUnDeletedOperators();
            this.DayGroupOperators = IDayGroupOperators.GetGroupOperators(ITradeHistory.GetDateTimeFromTradeHistoryId(tradeHistoryId));
            this.OperatorInfo = IOperatorInfo.GetOperatorInfo(operatorId);
            this.Operator = IOperator.GetModel(operatorId);
            this.UserGroup = IDayGroupOperators.GetGroup(operatorId, ITradeHistory.GetDateTimeFromTradeHistoryId(tradeHistoryId));
            this.Sort = IOperatorWeekSort.GetOperatorSort(tradeHistoryId, operatorId);
            this.OperatorMonthSum = IOperatorMonthSum.GetOperatorMonthSum(operatorId, th.MPdate, th.YPdate);
            DateTime pdate = ITradeHistory.GetLastMonth(th.MPdate, th.YPdate);
            this.PreOperatorMonthSum = IOperatorMonthSum.GetOperatorMonthSum(operatorId, pdate.Month, pdate.Year);
            this.LoginOperatorMonthExpected = IOperatorMonthExpected.GetOperatorMonthExpected(operatorId, th.YPdate, th.MPdate).DayCount;
            this.GrowthIndex = IOperator.GetGrowthIndex(PreOperatorMonthSum.Profit, OperatorMonthSum.Profit);
            this.StableIndex = IOperator.GetStableIndex(PreOperatorMonthSum.Profit, OperatorMonthSum.Profit);
            this.GrowthCount = IOperator.GetPicCount(GrowthIndex);
            this.StableCount = IOperator.GetPicCount(StableIndex);
            this.Lengths = GetMonthSumAllLength(OperatorMonthSum.Profit, PreOperatorMonthSum.Profit, this.LoginOperatorMonthExpected);
            DisposeInterface();
            /*
                            ViewBag.TradingDayPdate = TradingDayPdate;
             */
        }

        private void InitInterface(DB.DBManager db)
        {
            #region 初始化接口
            ITradeHistory = new Service.Sys.ServiceTradeHistory(db);
            IOperatorCouldSeeGroups = new Service.Sys.ServiceOperatorCouldSeeGroups(db);
            IOperator = new Service.Sys.ServiceOperator(db);
            IDayGroupOperators = new Service.Sys.ServiceDayGroupOperators(db);
            IOperatorInfo = new Service.Sys.ServiceOperatorInfo(db);
            IOperatorWeekSort = new Service.Week.ServiceOperatorWeekSort(db);
            IOperatorMonthSum = new Service.View.ServiceOperatorMonthSum(db);
            IOperatorMonthExpected = new Service.View.ServiceOperatorMonthExpected(db);
            #endregion
        }
        /// <summary>
        /// 释放接口
        /// </summary>
        private void DisposeInterface()
        {
            #region 释放接口
            ITradeHistory = null;
            IOperatorCouldSeeGroups = null;
            IOperator = null;
            IDayGroupOperators = null;
            IOperatorInfo = null;
            IOperatorWeekSort = null;
            IOperatorMonthSum = null;
            IOperatorMonthExpected = null;
            #endregion
        }


        /// <summary>
        /// 得到收益条的长度
        /// </summary>
        /// <param name="monthSum">本月当前盈利</param>
        /// <param name="lastMonthSum">上月盈利</param>
        /// <param name="expectedSum">本月预期盈利</param>
        /// <returns>数组，本月盈利条长度，上月盈利条长度，本月预期条长度</returns>
        private int[] GetMonthSumAllLength(double monthSum, double lastMonthSum, double expectedSum)
        {
            int[] lengths = new int[3];
            int maxLength = 80;
            if (monthSum == 0)
            {
                monthSum = 1;
            }
            if (expectedSum == 0)
            {
                expectedSum = 1;
            }
            if (lastMonthSum == 0)
            {
                lastMonthSum = 1;
            }
            lengths[0] = Convert.ToInt32((monthSum * maxLength) / expectedSum);
            lengths[1] = Convert.ToInt32((lastMonthSum * maxLength) / expectedSum);
            lengths[2] = maxLength;

            for (int i = 0; i < lengths.Length; i++)
            {
                if (lengths[i] <= 0)
                {
                    lengths[i] = 5;
                }
            }
            return lengths;
        }
    }
}
